<template>
  <div>
    <el-card style="margin:15px 0px">
      <CategorySelect @getInfoList="getSpuInfoList" :show="scene != 0"></CategorySelect>
    </el-card>
    <el-card>
      <!-- 这部分将来会有三部分切换 -->
      <div v-show="scene == 0">
        <!-- 展示SPU列表 -->
        <el-button type="primary" icon="el-icon-plus" :disabled="!category3Id" @click="addSpu">添加SPU</el-button>
        <el-table style="width: 100%" border="" :data="records">
          <el-table-column type="index" label="序号" width="80" align="center">
          </el-table-column>
          <el-table-column prop="spuName" label="spu名称" width="width">
          </el-table-column>
          <el-table-column prop="description" label="spu描述" width="width">
          </el-table-column>
          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{row, $index}">
              <hint-button type="primary" icon="el-icon-plus" size="mini" title="添加sku"
                @click="addSku(row)"></hint-button>
              <hint-button type="warning" icon="el-icon-edit" size="mini" title="修改spu"
                @click="updateSpu(row)"></hint-button>
              <hint-button type="info" icon="el-icon-info" size="mini" title="查看当前spu全部的sku列表"
                @click="handler(row)"></hint-button>
              <el-popconfirm :title="`确定删除${row.spuName}吗？`" @onConfirm="deleteSpu(row)">
                <hint-button type="danger" icon="el-icon-delete" size="mini" title="删除spu" slot="reference"></hint-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!--
             -->
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" style="text-align:center;"
          :current-page="page" :page-sizes="[5, 10, 20]" :page-size="limit"
          layout="prev, pager, next, jumper, ->, sizes, total" :total="total">
        </el-pagination>
      </div>
      <SpuForm v-show="scene == 1" ref="spu" @changeScene="changeScene">
        <!-- 添加或修改SPU的展示区 -->
      </SpuForm>
      <SkuForm v-show="scene == 2" ref="sku" @changeSceneFromSku="changeSceneFromSku">
        <!-- 展示添加SKU的结构 -->
      </SkuForm>
    </el-card>
    <el-dialog :title="`${spu.spuName}的sku的列表`" :visible.sync="dialogTableVisible" :before-close="handleClose">
      <el-table :data="skuList" style="width: 100%" border="" v-loading="loading">
        <el-table-column prop="skuName" label="名称" width="width">
        </el-table-column>
        <el-table-column prop="price" label="价格" width="width">
        </el-table-column>
        <el-table-column prop="weight" label="重量" width="width">
        </el-table-column>
        <el-table-column label="默认图片" width="width">
          <template slot-scope="{row,$index}">
              <img :src="row.skuDefaultImg" style="width:100px;height:100px;">
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import SpuForm from './spuForm'
import SkuForm from './skuForm'
export default {
  name: 'Spu',
  components: { SpuForm, SkuForm },

  data() {
    return {
      category1Id: '',
      category2Id: '',
      category3Id: '',
      page: 1,
      limit: 5,
      records: [],  //存储spu列表数据
      total: 0,  //分页器一共需要展示数据的条数
      scene: 0, //0:展示SPU列表数据页面；1:展示添加或修改SPU页面；2:展示添加SKU的结构页面
      dialogTableVisible: false,
      spu: {},
      skuList: [], // 存储的是sku列表的数据
      loading: true

    }
  },
  methods: {
    async getSpuInfoList({ category1Id, category2Id, category3Id }) {
      //根据category2Id,category3Id是否为空进行判断
      this.category1Id = category1Id
      this.category2Id = category2Id
      this.category3Id = category3Id

      const { page, limit } = this

      if (category2Id != '' && category3Id != '') {
        let result = await this.$API.spu.reqSpuList(page, limit, category3Id)
        if (result.code == 200) {
          this.total = result.data.total
          this.records = result.data.records
        }
      }

    },
    handleCurrentChange(val) {
      this.page = val
      const { category1Id, category2Id, category3Id } = this
      this.getSpuInfoList({ category1Id, category2Id, category3Id })
    },
    handleSizeChange(val) {
      this.limit = val
      const { category1Id, category2Id, category3Id } = this
      this.getSpuInfoList({ category1Id, category2Id, category3Id })
    },
    addSpu() {
      this.scene = 1
      // 通知子组件发请求(子组件发2个请求)
      this.$refs.spu.addSpuData(this.category3Id)
    },
    updateSpu(row) {
      this.scene = 1
      this.$refs.spu.initSpuData(row)
    },
    //spuform通知父组件修改场景
    changeScene({ scene, flag }) {
      //flag是为了区分保存按钮是添加还是修改
      // 切换结构（场景）
      this.scene = scene
      if (flag == 'edit') {
        this.page = this.page
      }
      if (flag == 'add') {
        this.page = 1
      }
      // 如果回到scene=0的页面，则重新获取spu列表数据
      if (scene == 0) {
        const { category1Id, category2Id, category3Id } = this
        this.getSpuInfoList({ category1Id, category2Id, category3Id })
      }
    },
    // 删除spu
    async deleteSpu(row) {
      const result = await this.$API.spu.reqDeleteSpu(row.id)
      if (result.code == 200) {
        this.$message({ type: 'success', message: '删除成功！' })
        const { category1Id, category2Id, category3Id } = this
        if (this.records.length <= 1) {
          this.page = this.page > 1 ? this.page - 1 : this.page
        }
        this.getSpuInfoList({ category1Id, category2Id, category3Id })
      } else {
        this.$message('删除失败！')
      }
    },
    // 添加sku按钮的回调
    addSku(row) {
      this.scene = 2
      this.$refs.sku.getData(this.category1Id, this.category2Id, row);
    },
    //skuform通知父组件修改场景
    changeSceneFromSku(scene) {
      this.scene = scene
    },
    // 查看sku列表的按钮的回调
    async handler(spu) {
      this.dialogTableVisible = true
      this.spu = spu
      let result = await this.$API.spu.reqSkuList(spu.id)
      if (result.code == 200) {
        this.skuList = result.data
        this.loading = false
      }
    },
    // 关闭对话框的回调
    handleClose(done){
      this.loading = true
      this.skuList = []
      done()
    }


  }

}
</script>

<style></style>
